package com.mmm.station.security.aop;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created by mmm on 2016/12/2.
 */
public class CglibProxyFactory implements MethodInterceptor {
    private Object targetObject;

    public Object createProxyInstance(Object target){
        this.targetObject = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.targetObject.getClass());
        //设置回调函数
        enhancer.setCallback(this);
        return enhancer.create();

    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args,
                            MethodProxy proxy) throws Throwable {

        System.out.println("record:"+method.getName());
        System.out.println("Object:"+obj.getClass());
        System.out.println("targetObject:"+targetObject.getClass());
//        return "fds";
//        throw new RoleException("没有相应的角色");
        return method.invoke(targetObject, args);
    }
}
